package com.javabase.y2022.m05.day0517.v8;

/**
 * @descriptions:
 * @author: Tom
 * @date: 2022/5/17 下午 04:21
 * @version: 1.0
 */
public class Solution {

    public static void main(String[] args) {
        int size = 12999;
        int[] arr1 = new int[size];
        int[] arr2 = new int[size];
        int[] arr3 = new int[size];
        for (int i = 0; i < size; i++) {
            int value = (int)Math.random()*size;
            arr1[i] = value;
            arr2[i] = value;
            arr3[i] = value;
        }
        quickSort1(arr1);
        quickSort2(arr2);
        quickSort3(arr3);

//        for (int i = 0; i < arr.length - 1; i++) {
//            System.out.println(" " + arr[i]);
//        }
    }

    public static void quickSort3(int[] arr){
        long start = System.currentTimeMillis();
        process3(arr, 0, arr.length - 1);
        long end = System.currentTimeMillis();
        System.out.println("duration3:" + (end - start));
    }
    public static void process3(int[] arr, int L, int R){
        if(L>=R){
            return;
        }
        int random = L + (int)(Math.random() * (R - L + 1));
        swap(arr, random, R);
        int[] equalArea = nethelandFlag(arr, L, R);
        process3(arr, L, equalArea[0] - 1);
        process3(arr, equalArea[1] + 1, R);
    }

    public static void swap(int[] arr, int i, int j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }

    public static int partition(int[] arr, int L, int R) {
        if (L > R) {
            return -1;
        }
        if (L == R) {
            return L;
        }
        int index = L;
        int lessEqual = L - 1;
        while (index < R) {
            if (arr[index] <= arr[R]) {
                swap(arr, index, ++lessEqual);
            }
            index++;
        }
        swap(arr, R, ++lessEqual);
        return lessEqual;
    }

    public static void quickSort1(int[] arr) {
        long start = System.currentTimeMillis();
        int L = 0, R = arr.length - 1;
        process1(arr, L, R);
        long end = System.currentTimeMillis();
        System.out.println("duration1:" + (end - start));
    }

    public static void process1(int[] arr, int L, int R) {
        if(L >= R){
            return;
        }
        int M = partition(arr, L, R);
        process1(arr, L, M - 1);
        process1(arr, M + 1, R);
    }

    public static void quickSort2(int[] arr){
        long start = System.currentTimeMillis();
        int L = 0, R = arr.length - 1;
        process2(arr, L, R);
        long end = System.currentTimeMillis();
        System.out.println("duration2:" + (end - start));
    }
    public static void process2(int[] arr, int L, int R){
        if(L >= R){
            return;
        }
        int[] equalArea = nethelandFlag(arr, L, R);
        process2(arr, L, equalArea[0]-1);
        process2(arr, equalArea[1]+1, R);
    }

    public static int[] nethelandFlag(int[] arr, int L, int R) {
        if (L > R) {
            return new int[]{-1, -1};
        }
        if (L == R) {
            return new int[]{L, R};
        }
        int less = L - 1;
        int index = L;
        int more = R;
        while (index < more) {
            if (arr[index] == arr[R]) {
                index++;
            } else if (arr[index] < arr[R]) {
                swap(arr, index++, ++less);
            } else {
                swap(arr, index, --more);
            }
        }
        swap(arr, R, more);
        return new int[]{less + 1, more};
    }
}
